function addMetric(idSource, idDestination, classDestination){
	var selectedSource = null;
	$("#" + idSource + " option").each(function(i, elem){
		if($(elem).attr("selected") == true){
			selectedSource = $(elem).text();
			$(elem).remove();
		}
	});
	
	if (selectedSource != null){
		var optionDestination = $("<tr><td><a onclick=\"removeMetric(this)\"><img src=\"../images/icon-remove.png\" width=\"10px;\"  border=\"0\" /></a></td><td>" + selectedSource + "</td><td>Weight: <input type='text' size=3 /><td></tr>").addClass(classDestination).data("option", selectedSource);
		var elemDestination = $("#" + idDestination);
		elemDestination.append(optionDestination);
	}
}

function removeMetric(elem){
	$(elem).parent().parent().remove();
}

function calculateModel() {
	var elemProject = $("#projectName");
	
	var metricsSize = new Array();
	var weightsSize = new Array();
	var i = 0;
	$(".classSize").each(function(i, elem){
		var metric = $(this).data("option");
		var weight = $("input", this).val();
			
		metricsSize[i] = metric;
		weightsSize[i] = weight;
		i++;
	});
	
	var metricsQuality = new Array();
	var weightsQuality = new Array();
	var i = 0;
	$(".classQuality").each(function(i, elem){
		var metric = $(this).data("option");
		var weight = $("input", this).val(); 
			
		metricsQuality[i] = metric;
		weightsQuality[i] = weight;
		i++;
	});
	
	if ((metricsSize.length > 0 || metricsQuality.length > 0)){
		if (validateValues(weightsSize) && validateValues(weightsQuality)){
			$.blockUI({ css: {
				border: 'none', 
		        backgroundColor: '#000', 
		        '-webkit-border-radius': '10px', 
		        '-moz-border-radius': '10px', 
		        opacity: .5, 
		        color: '#000'
		    } });
			
			Model.getModelResult(elemProject.val(), metricsSize, weightsSize, metricsQuality, weightsQuality, updateResultsModel);
		}
	} else {
		alert("There aren't selected metrics");
	}
}

function validateValues(array){
	var total = 0.0;
	for (var i = 0; i < array.length; i++) {
		var value = array[i];
		if (value == ""){
			alert("One of the weights is blank");
			return false;
		} else {
			total = total + parseFloat(value);
		}
	}
	if (array.length > 0 && total != 100){
		alert("The weights doesn't sum 100%");
		return false;
	}
	return true;
}

function updateResultsModel(data) {
	var elemTable = $("#results-table");
	elemTable.empty();
	
	elemTable.append("<tr><th>DEVELOPER</th><th>% OF CONTRIBUTION SIZE</th><th>% OF CONTRIBUTION QUALITY</th></tr>");
	for (var i = 0; i < data.length - 1; i++) {
		var row = data[i];
		var textRow = "<tr>";
		
		var columns = row.split("%%");
		for (var j = 0; j < columns.length; j++) {
			var column = new String (columns[j]);
			var color = "";
			if (column.indexOf("**") >= 0){
				column = column.replace("**", "");
				color = "red";
			}
			textRow += "<td style=\"color:" + color + ";\">" + column + "</td>";
		}
		
		textRow += "</tr>";
		elemTable.append(textRow);
	}
	elemTable.append("<tr><th>TOTAL</th><th>100.00</th><th>100.00</th></tr>");

	var keyData = data[data.length - 1];
	var elemButton = $("#results-button");
	elemButton.attr("onclick", "javascript:window.open('/devmeter/report/reportModel.action?keyData="+ keyData +"','_self')");
	
	$.blockUI({ message: $('#results-div') });
}

function hideResultsTable() {
	setTimeout($.unblockUI, 1);
}